<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	private $appid = 'wx893194fd29941685';
	private $appsecret = '677ec003845d567a748c5659231a3cc5';

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

	public function index()
	{
		$this->load->view('index');
	}

	// public function index2()
	// {
	// 	$this->load->view('index2');
	// }

	//保存用户信息
	public function save_userinfo(){
		
		$username    = $this->input->post('username');
		$sex         = $this->input->post('sex');
		$phone       = $this->input->post('phone');
		$province    = $this->input->post('province');
		$city        = $this->input->post('city');
		$company     = $this->input->post('company');
		$post        = $this->input->post('post');
		$hobby       = $this->input->post('hobby');
		$declaration = $this->input->post('declaration');
		$headimgurl  = $this->input->post('headimgurl');

		
		
		if (strlen(trim($username))===0 || strlen(trim($username))>20) {
			exit(json_encode(array('status'=>'username')));
		}

		if (strlen(trim($sex))===0 || strlen(trim($sex))>10) {
			exit(json_encode(array('status'=>'sex')));
		}

		if (strlen(trim($phone))===0 || strlen(trim($phone))>11) {
			exit(json_encode(array('status'=>'phone')));
		}

		if (strlen(trim($province))===0 || strlen(trim($province))>20) {
			exit(json_encode(array('status'=>'province')));
		}

		// if (strlen(trim($city))===0 || strlen(trim($city))>20) {
		// 	exit(json_encode(array('status'=>'city')));
		// }

		if (strlen(trim($company))===0 || strlen(trim($company))>50) {
			exit(json_encode(array('status'=>'company')));
		}

		if (strlen(trim($post))===0 || strlen(trim($post))>20) {
			exit(json_encode(array('status'=>'post')));
		}

		if (strlen(trim($hobby))===0 || strlen(trim($hobby))>200) {
			exit(json_encode(array('status'=>'hobby')));
		}

		if (strlen(trim($declaration))===0 || strlen(trim($declaration))>200) {
			exit(json_encode(array('status'=>'declaration')));
		}

		if (strlen(trim($headimgurl))>200) {
			exit(json_encode(array('status'=>'headimgurl')));
		}

		
		$user = $this->db->get_where('member',array('phone'=>$phone))->row_array();
		if (!empty($user)) {
			$data = $this->input->post(NULL,true);

			$this->db->where('phone', $data['phone']);
			
			if ($this->db->update('member', $data)) {
				echo json_encode(array('status'=>'success'));
			}else{
				echo json_encode(array('status'=>'fail'));
			}
			
		}else{

			$data = $this->input->post(NULL,true);
			if ($this->db->insert('member', $data)) {
				echo json_encode(array('status'=>'success'));
			}else{
				echo json_encode(array('status'=>'fail'));
			}	
		}



					
	}


	//查找某一用户信息
	public function detail($id = 0){
		$user = $this->db->get_where('member', array('id'=>(int)$id))->row_array();
		if (empty($user)) {
			echo 'no user';
		}else{
			$this->load->view('detail',array('user'=>$user));	
		}
	}


	public function to_vote(){
		$this->load->view('vote');	
	}


	//上传图片
	public function upload(){
		$img  = $this->input->post('img');

		// echo $img;exit;
		$img  = str_replace('data:image/png;base64,', '', $img);
		$img  = str_replace(' ', '+', $img);
		$data = base64_decode($img);
         
        $save_file = './uploads/'.uniqid().'.png';
        $imagePath = 'uploads/'.uniqid().'.png';
        $result = file_put_contents($save_file, $data);

        echo json_encode(array('status'=>'success','imagePath'=>$imagePath));
    }


    public function province(){
    	$pro = $this->db->order_by('sort')->get_where('area',array('parent_id'=>0))->result_array();
    	echo json_encode($pro);
    }

    public function city($id=2600){
    	$city = $this->db->get_where('area',array('parent_id'=>(int)$id))->result_array();
    	if (empty($city)) {
    		$city = $this->db->get_where('area',array('id'=>(int)$id))->result_array();
    	}
    	echo json_encode($city);
    }


    public function to_oauth(){

    	$redirect_url = urlencode(site_url('welcome/get_code'));
		$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
		header("Location:".$url);
    }

    public function get_code(){

		$code = $this->input->get('code');
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";

		$result = (array)json_decode(file_get_contents($url));

		if (isset($result['openid'])) {
			$openid       =  $result['openid'];
			$access_token =  $result['access_token'];
			$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
			$wechat_user = (array)json_decode(file_get_contents($url));
			unset($wechat_user['privilege']);		
			unset($wechat_user['unionid']);	

			$user = $this->db->get_where('wechat_user', array('openid'=>$openid))->row_array();
			if (empty($user)) {
				if ($this->db->insert('wechat_user', $wechat_user)) {
					echo "success";
				}else{
					echo 'fail';
				}
			}else{
				unset($wechat_user['openid']);	
				$this->db->where('openid', $openid);
				$this->db->update('wechat_user', $wechat_user);
			}
		}
	}
    
}
